/**
 * 
 */
package mdb.com.vn.persist;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import org.springframework.stereotype.Repository;

import mdb.com.vn.model.Casa;

/**
 * @author tu.dod
 *
 */
@Repository("casaDAO")
public class CasaDAOImpl extends AbstractHibernateDAOImpl<Casa, Integer> implements CasaDAO,Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	public List<Casa> findByPersionalType(int type) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("Casa.findByPersionalType").setInteger("customerType", type).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Casa> findByCorporateType(int type) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("Casa.findByPersionalType").setInteger("customerType", type).list();
	}

	@Override
	protected Class<Casa> getDomainClass() {
		// TODO Auto-generated method stub
		return Casa.class;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Casa> findByCode(String code) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("Casa.findByCode").setString("accountLocked", code).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getSCardCSAvailable(String customerRef) {
		// TODO Auto-generated method stub
		List<Casa> casas = getSession().getNamedQuery("Casa.findByCustomerRef").setString("customerId", customerRef).list();
		int totalCSAvailable = 0;
		for (Casa casa : casas) {
			totalCSAvailable+=casa.getScCardNotGet();
		}
		return totalCSAvailable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Casa> findByCustomerRef(String customerId) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("Casa.findByCustomerRef").setString("customerId", customerId).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getSCardCSAvailableException(String customerRef,
			String accountException) {
		List<Casa> casas = getSession().getNamedQuery("Casa.findByCustomerRef").setString("customerId", customerRef).list();
		int totalCSAvailable = 0;
		for (Casa casa : casas) {
			if(!casa.getAccountLocked().equals(accountException)){
				totalCSAvailable+=casa.getScCardNotGet();
			}
			
		}
		return totalCSAvailable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Casa> getAllCasas() {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("Casa.getAllCasas").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countThanksCard() {
		// TODO Auto-generated method stub
		List<Long> longs = getSession().getNamedQuery("Casa.countThanksCard").list();
		if (longs.size() > 0){
			if(longs.get(0)!=null){
				return longs.get(0).intValue();
			}
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public BigDecimal sumAmount() {
		// TODO Auto-generated method stub
		List<Double> longs = getSession().getNamedQuery("Casa.sumAmount").list();
		if (longs.size() > 0){
			if(longs.get(0)!=null){
				return new BigDecimal(longs.get(0)) ;
			}
		}
		return new BigDecimal(0);
	}

}
